package com.create404.suishoupai.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.create404.suishoupai.dto.CommentedNewsDto;
import com.create404.suishoupai.dto.PageVo;
import com.create404.suishoupai.entity.NewsComment;
import com.create404.suishoupai.service.INewsCommentService;
import com.create404.suishoupai.utils.Result;
import com.create404.suishoupai.vo.NewsCommentListItemVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author create404
 * @since 2021-07-17
 */
@Api(tags = "新闻评论接口")
@RestController
@RequestMapping("/news-comment")
public class NewsCommentController {
    @Autowired
    private INewsCommentService iNewsCommentService;

    @GetMapping("/newsCommentList")
    public Result getNewsCommentList(String newsId) {
        List<NewsCommentListItemVO> newsCommentList = iNewsCommentService.getNewsCommentList(newsId);
        return Result.ok(newsCommentList);
    }

    @PostMapping("/addNewsComment")
    public Result addNewsComment(String newsId, String employeeId, String content) {
        NewsComment newsComment = new NewsComment();
        newsComment.setNewsId(newsId);
        newsComment.setEmployeeId(employeeId);
        newsComment.setContent(content);
        newsComment.setCreateTime(LocalDateTime.now());
        newsComment.setUpdateTime(LocalDateTime.now());
        newsComment.setTime(LocalDateTime.now());
        boolean result = iNewsCommentService.save(newsComment);
        if (result) {
            return Result.ok("评论成功");
        } else {
            return Result.error("评论失败");
        }
    }

    @PostMapping("/getCommentedNews")
    public Result getCommentedNews(@RequestBody PageVo pageVo) {
        String employeeId = pageVo.getEmployeeId();
        Integer currentPage = Integer.valueOf(pageVo.getPageNum());
        Integer pageSize = Integer.valueOf(pageVo.getPageSize());
        Page<CommentedNewsDto> commentedNewsDtoPage = new Page<CommentedNewsDto>(currentPage, pageSize);
        IPage<CommentedNewsDto> page = iNewsCommentService.getCommentedNews(commentedNewsDtoPage, employeeId);
        return Result.ok(page);
    }


}
